package svc

import (
	"fmt"
	"lottery-zero/internal/config"
	"lottery-zero/internal/middleware"

	"github.com/tal-tech/go-zero/rest"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type ServiceContext struct {
	Config          config.Config
	UserIdHeaderMix rest.Middleware
	PGDB            *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := gorm.Open(postgres.Open(c.Pgdns), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败")
		return nil
	}
	return &ServiceContext{
		Config:          c,
		UserIdHeaderMix: middleware.NewUserIdHeaderMixMiddleware().Handle,
		PGDB:            db,
	}
}
